<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataMapper Models : DataMapper ORM - User Guide</title>

<link rel="shortcut icon" type="image/png" href="../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
DataMapper Models
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>DataMapper Models</h1>

<p>In order for DataMapper to map your Database tables into objects, you first need to create a DataMapper model for each table.  These models will extend DataMapper in order to gain the wonderful functionality of tables as objects.</p>

<p>
	DataMapper Models are <strong>very</strong> different than <a href="http://codeigniter.com/user_guide/general/models.html">CodeIgniter Models</a>.
	Unlike CI models, there is no need to load them explicitly, Datamapper ORM handles that automatically. And they should never be added to <strong>autoload</strong>.
</p>

<h2 id="Basic.Template">Basic Template</h2>
<div class="example">
	<h4>Template Available</h4>
	<p>Datamapper ORM comes packaged with a ready-to-use base template:</p>
	<div class="paths">ZIP/<b>application/<wbr/>models/<wbr/>_template.php</b></div>
</div>
<p>Below is a basic template you can use to create DataMapper models.</p>
<ul>
	<li><var>Name</var> - Replace this value with the name of your object.  For example: <var>User</var></li>
	<li><var><u>DataMapper</u></var> - Extending DataMapper is what makes your model a DataMapper model.</li>
	<li><kbd>__construct</kbd> - (Optional) It is highly recommended that you use this standard PHP constructor, instead of the class name, for easier management later.  If you want the ability to load a model by ID when it is created, make sure you include the <kbd>$id</kbd> parameter.</li>
<pre>
<kbd>class </kbd><var>Name </var><kbd>extends </kbd><var><u>DataMapper</u> </var><kbd>{

    </kbd><samp>// Optionally, don't include a constructor if you don't need one.
    </samp><kbd>function </kbd><var><dfn>__construct</dfn></var><kbd>(</kbd><var>$id </var><kbd>= </kbd><var>NULL</var><kbd>)
    {
        </kbd><var><dfn>parent</dfn></var><kbd>::</kbd><var><dfn>__construct</dfn></var><kbd>(</kbd><var>$id</var><kbd>);
    }

    </kbd><samp>// Optionally, you can add post model initialisation code
    </samp><kbd>function </kbd><var><dfn>post_model_init</dfn></var><kbd>(</kbd><var>$from_cache </var><kbd>= </kbd><var>FALSE</var><kbd>)
    {
    }
}

</kbd><samp>/* End of file <var>name</var>.php */
/* Location: ./application/models/<var>name</var>.php */</samp>
</pre>

<div class="note">
	<p>If you define a constructor, but do not pass in the $id value, you will not be able to use the shorthand:</p>
	<pre><var>$user </var><kbd>= new </kbd><var>User</var><kbd>(</kbd><var>$user_id</var><kbd>);</kbd></pre>
	<p>Instead, you will still need to use the original method:</p>
	<pre>
<var>$user </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><var>$user</var><kbd>-&gt;</kbd><var><u>get_by_id</u></var><kbd>(</kbd><var>$user_id</var><kbd>);</kbd></pre>
</div>
	<li><kbd>post_model_init</kbd> - (Optional) After Datamapper has loaded and initialized the model, it calls the post_model_init() method (if defined), where you can add initialisation code specific for this model. The <kbd>$from_cache</kbd> parameter indicates if the current model configuration was generated, or was loaded from the <a href="prodcache.html">production cache</a>.</li>
</ul>


<h2 id="rules">Rules</h2>

<p>DataMapper models must be named the singular version of the object name, with an uppercase first letter. So for a user object, the DataMapper model would be named <b>User</b>.  The model should have a corresponding table in the database named as the lowercase, pluralised version of the object name.  So for a DataMapper model named <b>User</b>, the table would be named <b>users</b>.  For a DataMapper model named <b>Country</b>, the table would be named <b>countries</b>.</p>

<p>In most cases, the difference between the singular and plural version of an object name is just a matter of adding the letter <b>s</b> on the end.  For example:</p>

<table cellpadding="0" cellspacing="1" border="0" class="tableborder">
<tr>
	<th>Model name</th>
	<th>Table name</th>

</tr>
<tr>
	<td>Author</td>
	<td>authors</td>
</tr>
<tr>
	<td>Book</td>
	<td>books</td>
</tr>
<tr>
	<td>Genre</td>
	<td>genres</td>
</tr>
</table>

<p>However, some object names have completely different wording between the singular and plural. For example:</p>

<table cellpadding="0" cellspacing="1" border="0" class="tableborder">
<tr>
	<th>Model name</th>
	<th>Table name</th>

</tr>
<tr>
	<td>Country</td>
	<td>countries</td>
</tr>
<tr>
	<td>Person</td>
	<td>People</td>
</tr>
</table>

<p>In this case, you will need to specify the table name in your DataMapper model.  You do this by adding a class variable of <var><i>$table</i></var>, which should be the name of your table. For example:</p>

<pre>
<kbd>class </kbd><var>Country </var><kbd>extends </kbd><var><u>DataMapper</u> </var><kbd>{

    var </kbd><var><i>$table</i> </var><kbd>= </kbd><dfn>'countries'</dfn><kbd>;

    function </kbd><var><dfn>__construct</dfn></var><kbd>(</kbd><var>$id </var><kbd>= </kbd><var>NULL</var><kbd>)
    {
        </kbd><var><dfn>parent</dfn></var><kbd>::</kbd><var><dfn>__construct</dfn></var><kbd>(</kbd><var>$id</var><kbd>);
    }
}

</kbd><samp>/* End of file country.php */
/* Location: ./application/models/country.php */</samp>
</pre>

<p>If you don't supply the <var><i>$table</i></var> variable, DataMapper will automatically assume the table name is the same as your model name, in lowercase, with the letter <b>s</b> on the end (which will be the case most of the time).</p>
<p>However, with that said, I have included a customised version of CodeIgniter's <strong>Inflector Helper</strong> with DataMapper that should be able to correctly convert most irregular singular/plural words, if loaded.</p>

<p>Most english words should be covered by the updated inflector helper.  If you are still having issues, please contact me (see <a href="troubleshooting.html">Troubleshooting</a>) and I'll try to update the inflector helper.</p>

<p class="note">There is one other scenario to look at where the singular and plural name of an object can get a little confusing.  What do you do if the singular name of an object is the same as the plural name?  For example, the word <b>fruit</b> is used for both a single piece of fruit and multiple pieces of fruit.  In this case, you will have to use the singular model name of <b>Fruit</b> and the plural table name of <b>fruits</b>.  Alternatively, you can specify a different table name to the automatically determined name, in the same way as done above.</p>


</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagespath: ''
		});

	});
//-->
</script>
</body>
</html>
